/**
 * @filename:MoldChainController Jan 14, 2025
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2020 Wonder Co. Ltd.
 * All right reserved.
 */
package com.wonder.app.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wonder.app.domain.MoldAndInspect;
import com.wonder.app.domain.MoldScrap;
import com.wonder.app.request.MoldAndInspectRequest;
import com.wonder.app.request.MoldScrapRequest;
import com.wonder.app.service.MoldAndInspectService;
import com.wonder.app.service.MoldScrapService;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * <p>说明： 仓位列表API接口层</P>
 * @version: 1.1.0
 * @author: Wonder
 * @time    Jan 14, 2025
 *
 */
@Api(tags = "模具链列表")
@RestController
@RequestMapping("/app/MoldScrap")
public class MoldScrapController {

@Autowired
private MoldScrapService moldScrapService;

    @ApiOperation(value = "模具报废单列表")
    @RequestMapping(value = "/index", method = RequestMethod.POST)
    public Map<String,Object> index(@RequestBody MoldScrapRequest moldScrapRequest) {
        Page<MoldScrap> page = new Page<MoldScrap>(moldScrapRequest.getPage(), moldScrapRequest.getLimit());
        IPage<MoldScrap> list = moldScrapService.selectList(moldScrapRequest, page);

        Map<String,Object> retMap=new HashMap<>();
        retMap.put("data",CommonPage.restPage(list));


        return AjaxMap.success(retMap);
    }

    /**
     * @explain 修改排序开关
     * @param   entity
     * @return  Boolean
     * @author  Wonder
     */
    @PostMapping("/updateExt")
    @ApiOperation(value = "修改排序开关", notes = "作者：Wonder")
    public AjaxResult<MoldScrap> updateExt(@RequestBody MoldScrap entity){
        if (null!=entity) {
            boolean rsg =  moldScrapService.updateById(entity);
            if (rsg) {
                return AjaxResult.success("system.updateSuccess");
            }else {
                return AjaxResult.failed("system.updateFailed");
            }
        }else {
            return AjaxResult.failed("system.parameterError");
        }
    }

    /**
     * @explain 添加
     * @param   entity
     * @return  Boolean
     * @author  Wonder
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加", notes = "作者：Wonder")
    public AjaxResult<MoldScrap> add(@RequestBody MoldScrap entity){

        if (null!=entity) {

            String validate=moldScrapService.validate(entity,"new");
            if(StringUtils.hasText(validate))
                return AjaxResult.failed(validate);
            boolean rsg = moldScrapService.save(entity);
            if (rsg) {
                return AjaxResult.success("system.insertSuccess");
            }else {
                return AjaxResult.failed("system.insertFailed");
            }
        }else {
            return AjaxResult.failed("system.parameterError");
        }
    }
    /**
     * @explain 修改
     * @param   entity
     * @return  Boolean
     * @author  Wonder
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "作者：Wonder")
    public AjaxResult<MoldScrap> update(@RequestBody MoldScrap entity){
        if (null!=entity) {
            String validate=moldScrapService.validate(entity,"update");
            if(StringUtils.hasText(validate))
                return AjaxResult.failed(validate);
            boolean rsg = moldScrapService.updateById(entity);
            if (rsg) {
                return AjaxResult.success("system.updateSuccess");
            }else {
                return AjaxResult.failed("system.updateFailed");
            }
        }else {
            return AjaxResult.failed("system.parameterError");
        }
    }

    /**
     * @explain 删除
     * @param   map 对象参数：id
     * @return  JsonResult
     * @author  Wonder
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除", notes = "作者：Wonder")
    public AjaxResult<MoldScrap> delete(@RequestBody Map<String,Object> map){
        if(map==null)return AjaxResult.failed("system.parameterError");
        String ids=map.get("id").toString();
        ids=StringUtils.remove(ids, '[');
        ids=StringUtils.remove(ids, ']');
        String[] deleteIds=StringUtils.split(ids, ',');
        List<Long> idList=new ArrayList<>();
        for(int i=0;i<deleteIds.length;i++)
        {
            idList.add( Long.parseLong(deleteIds[i].trim()));//整数数组
        }

        boolean rsg = moldScrapService.removeBatchByIds(idList);
        if (rsg) {
            return AjaxResult.success("system.deleteSuccess");
        }else {
            return AjaxResult.failed("system.deleteFailed");
        }

    }
}
